﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using System.Threading;
using WatiN.Core.Interfaces;

namespace WatiNCF.Extensions
{
    public static class BrowserExtensions
    {
        /// <summary>
        /// Waits for async post back to complete.
        /// </summary>
        /// <param name="ie">The ie instance to use.</param>
        public static void WaitForAsyncPostBackToComplete(this IE ie)
        {
            bool isInPostback = true;
            while (isInPostback)
            {
                isInPostback = Convert.ToBoolean(ie.Eval("Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();"));
                if (isInPostback)
                {
                    Thread.Sleep(200); //sleep for 200ms and query again 
                }
            }
        }

        public static void CaptureWebPageToFile(this IE browser, string fileName)
        {
            ((IE)browser).CaptureWebPageToFile(fileName);
        }

        public static void Close(this IE browser)
        {
            browser.Close();
        }
    }
}
